<?php

namespace app\admin\controller;

class Image extends AdminBase
{
    public function upload()
    {
        if (!$this->request->isPost()) {
            return show(config('status.error'),'请求不合法');
        }

        $file =  $this->request->file('file');

        $data = [
            'file_type' => $file->getMime(),
            'file_size' => $file->getSize(),
        ];

        //限制文件类型和文件大小
        $validate = new \app\admin\validate\Image();
        if (!$validate->check($data)) {
            return show(config('status.error'),$validate->getError());
        }


        $filename = \think\facade\Filesystem::disk('public')->putFile('upload/images', $file);

        if (!$filename) {
            return show(config('status.error'),'上传图片失败');
        }

        $url = '/storage/'.$filename;
        return show(config('status.success'),'OK',[
            'image' => $url,
        ]);
    }


    public function layUpload()
    {
        if (!$this->request->isPost()) {
            return show(config('status.error'),'请求不合法');
        }

        $file =  $this->request->file('file');

        $data = [
            'file_type' => $file->getMime(),
            'file_size' => $file->getSize(),
        ];

        $result = [
            'code' => 1,
            'data' => [],
        ];

        //限制文件类型和文件大小
        $validate = new \app\admin\validate\Image();
        if (!$validate->check($data)) {
            return json($result,200);
        }


        $filename = \think\facade\Filesystem::disk('public')->putFile('upload/images', $file);

        if (!$filename) {
            return show(config('status.error'),'上传图片失败');
        }

        $data = [
            'src' => '/storage/'.$filename
        ];
        $result['code'] = 0;
        $result['data'] = $data;
        return json($result, 200);
    }
}